/**
 * ---------------------------
 * Time: 2017/5/18 21:41.
 * Author: Cangshi
 * View: http://palerock.cn
 * ---------------------------
 */
var CanvasEffect = Main.createConfigClass({
    default: {
        defaultConfig: function () {
            return {
                type: 'simple', // 'particles'
                particles: {
                    maxSize: 20,
                    intervalTime: 100,
                    particleList: [],
                    config: {
                        type: 'simple'
                    }
                },
                effect: {
                    type: 'rect',
                    color: {type: 'random'},
                    width: 1,
                    height: 1,
                    degFaced: 0,
                    isFaceDeg: true,
                    rotate: {
                        isRotate: false,
                        frameDeg: 0
                    },
                    gradient: {
                        isGradually: true,
                        rate: 0.99
                    }
                },
                runningTrack: {
                    centrePoint: {
                        x: 0,
                        y: 0
                    },
                    direction: Math.random() * 360,
                    speed: 1
                }
            }
        },
        onConfigured: function () {
            this.type = this.config.type;
            switch (this.config.type) {
                case 'particles':
                    this._configParticles();
                    break;
                case 'simple':
                default:
                    this._configSimple();
                    break;
            }
        },
        _configParticles: function () {
            this.runningTrack = this.parent.runningTrack;
            this.particles = this.config.particles;
            this.particles.config.type = this.config.particles.config.type;
            this.particles.config.effect = this.config.effect;
            this.particles.config.runningTrack = this.config.runningTrack;
        },
        _configSimple: function () {
            this.id = Cielar.Utils.buildUniqueId();
            this.effect = this.config.effect.cloneShallow();
            this.effect.color = new Cielar.Color(this.config.effect.color);
            this.runningTrack = new RunningTrack(this.config.runningTrack);
        }
    },
    defaultPrototype: {
        _buildParticle: function () {
            this.particles.particleList.listArray(function (i) {
                if (this[i].isDestory) {
                    delete this[i];
                }
            });
            this.particles.particleList.removeAimInArray(null);
            if (this.particles.particleList.length > this.particles.maxSize) {
                return;
            }
            this.particles.config.runningTrack.centrePoint = this.runningTrack.centrePoint;
            this.particles.config.runningTrack.direction = Math.random() * 360;
            var particle = new this.constructor(this.particles.config);
            this.particles.particleList.push(particle);
            particle.startEffect();
        },
        startEffect: function () {
            var _this = this;
            if (this.type == 'particles') {
                _this._buildParticle();
                this.buildHandle = window.setInterval(function () {
                    _this._buildParticle();
                }, this.particles.intervalTime);
                return;
            }
            if (this.effect.gradient.isGradually) {
                this.runningTrack.doChanges = function () {
                    _this.effect.color.a = _this.effect.color.a * _this.effect.gradient.rate;
                    if (_this.effect.color.a - 0.01 <= 0) {
                        _this._destroy();
                    }
                };
            }
            this.runningTrack.runningStart();
        },
        stopEffect: function () {
            var _this = this;
            this.runningTrack.runningEnd();
            if (this.buildHandle) {
                window.clearInterval(this.buildHandle);
            }
        },
        _destroy: function () {
            this.stopEffect();
            this.isDestory = true;
            this.onDestroy();
        },
        onDestroy: function () {
        },
        getDrawConfig: function () {
            var config = {
                type: this.type
            };
            switch (this.type) {
                case 'particles':
                    config.particleList = this.particles.particleList;
                    return config;
                    break;
                case 'simple':
                    config.expandAll(this.runningTrack.getCurrentPoint());
                    var effect = this.effect;
                    config.expandAll({
                        effect: {
                            type: effect.type
                        },
                        width: effect.width,
                        height: effect.height,
                        degFaced: effect.degFaced,
                        isFaceDeg: effect.isFaceDeg,
                        rotate: effect.rotate,
                        color: effect.color.getRGBA()
                    });
                    return config;
                    break;
                default:
                    return {};
                    break;
            }
        }
    }
});